﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AfterCaveStory
{
    class AnimatedObjetc : Objeto2D
    {
        private AnimationStructure animation;
        private List<AnimationStructure> animationList;

        public struct AnimationStructure
        {
            public int frameWidth;
            public int frameHeight;
            public int frameCount;
            public int framesPerSecond;
            public int X;
            public int Y;
            public string name;
        }

        public AnimatedObjetc(Texture2D textura, Nullable<Rectangle> sourceRectangle, float depth)
            : base(textura, sourceRectangle, depth)
        {

            animationList = new List<AnimationStructure>();

            animation = new AnimationStructure();
            animation.name = "default";
            animation.frameCount = 1;
            animation.X = 0;
            animation.Y = 0;
            animation.framesPerSecond = 2;
            if (sourceRectangle != null)
            {
                Rectangle rectangle = (Rectangle)sourceRectangle;
                animation.frameWidth = rectangle.Width;
                animation.frameHeight = rectangle.Height;
            }
            else
            {
                animation.frameWidth = textura.Width;
                animation.frameHeight = textura.Height;
            }

            animationList.Add(animation);

        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            int frame = (int)(gameTime.TotalGameTime.TotalSeconds * animation.framesPerSecond) % animation.frameCount;

            sourceRectangle = new Rectangle(
                            frame * animation.frameWidth,
                            animation.Y,
                            animation.frameWidth,
                            animation.frameHeight);

            base.draw(gameTime, spriteBatch);
        }

        public String Animation
        {
            get
            {
                return animation.name;
            }
            set
            {
                for (int a = 0; a < animationList.Count; a++)
                {
                    if (animationList[a].name == value)
                    {
                        animation = animationList[a];
                    }

                }
            }
        }

        public void addAnimation(AnimationStructure animation)
        {
            animationList.Add(animation);
        }

    }
}
